<template>
    <div>
      <!--  面包屑导航区域-->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
  <!-- 卡片视图 -->
  <el-card>
<!--    搜素框-->
    <el-row :gutter="20">
      <el-col :span="7">
        <div class="grid-content bg-purple">
          <el-input placeholder="请输入你要搜索的关键字" v-model="queryInfo.query" clearable
                    @clear="getGoodList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodList"></el-button>
          </el-input>
        </div>
      </el-col>
      <el-col :span="4"><div class="grid-content bg-purple">
        <el-button type="primary" @click="goAddGoodsPage">添加商品</el-button>
      </div></el-col>
    </el-row>
<!--    表格区域-->
    <el-table :data="goodList" border stripe>
      <!-- stripe: 斑马条纹border：边框-->
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="goods_name" label="商品名称"></el-table-column>
      <el-table-column prop="goods_price" label="商品价格（元）" width="95px"></el-table-column>
      <el-table-column prop="goods_weight" label="商品重量"  width="95px"></el-table-column>
      <el-table-column prop="add_time" label="创建时间" width="150px">
        <template slot-scope="scope">
<!--          // | dataFormat 时间格式过滤器-->
          {{scope.row.add_time | dataFormat}}
        </template>
      </el-table-column>
        <!--    作用域插槽    scope.row得到这一行所有数据
         @change="userStateChanged(scope.row)监听变化 触发函数-->
      <el-table-column label="操作" width="120px">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            circle
            @click="goEditGoodsPage(scope.row.goods_id)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            circle
            @click="removeUserById(scope.row.goods_id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="this.queryInfo.pagenum"
      :page-sizes="[5, 10, 20]"
      :page-size="this.queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="this.total">
    </el-pagination>
  </el-card>
    </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取商品列表查询参数对象
      queryInfo: {
        query: '',
        // 当前页数
        pagenum: 1,
        // 每页显示多少数据
        pagesize: 10
      },
      // 商品列表
      goodList: [],
      // 商品总数
      total: 0
    }
  },
  created () {
    this.getGoodList()
  },
  methods:
    {
      async getGoodList () {
        const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
        if (res.meta.status !== 200) {
          this.$message.error('获取商品列表失败')
        }
        console.log(res)
        this.goodList = res.data.goods
        this.total = res.data.total
      },
      // 监听当前页是否变化时间
      handleCurrentChange(currentPage) {
        // console.log(currentPage)
        this.queryInfo.pagenum = currentPage
        this.getGoodList()
      },
      // 监听 每页显示多少数据
      handleSizeChange(pageSize) {
        // console.log(pageSize)
        this.queryInfo.pagesize = pageSize
        this.getGoodList()
      },
      // 通过路由跳到添加商品主界面（并非弹框）
      goAddGoodsPage() {
        this.$router.push({ name: 'Add' })
      },
      // 通过路由跳到编辑商品主界面（并非弹框）
      goEditGoodsPage(id) {
        // alert(id)
        this.$router.push({
          name: 'Edit',
          params: {
            goodId: id
          }

        })
      },
      // 删除商品
      async removeUserById (goodId) {
        const confirmRes = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
        // alert(confirmRes)
        if (confirmRes === 'confirm') {
          const { data: res } = await this.$http.delete('goods/' + goodId)
          console.log(res)
          if (res.meta.status !== 200) {
            return this.$message.error('删除商品失败！')
          }
          this.$message.success('删除商品成功！')
          this.getGoodList()
        }
      }

    }
}
</script>

<style lang="less" scoped>

</style>
